package com.jt.tenant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.conn.GetFreeWayOrder;
import com.jt.tenant.widget.Tools;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

//import com.jt.tenant.conn.GetSoftwareUpdate;

/**
 * Created by Administrator on 2016/2/14.
 */
public class FreewayServiceActivity extends JTActivity implements View.OnClickListener {

    private TextView title_name;

    private TextView state, username, plate,licensee;//用户状态，用户名，车牌号，驾驶证号

    private TextView tv_mileage,tv_time,tv_money;//总里程、时长、金额

    private TextView check_order;

    private String date;

    private double meilage = 0;

    private double price = 0.0;

    private long time = 0;

    private List<GetFreeWayOrder.Item> orderList = new ArrayList<>();

    private GetFreeWayOrder getFreeWayOrder = new GetFreeWayOrder(new AsyCallBack<GetFreeWayOrder.Info>(){

        @Override
        public void onSuccess(String toast, int type, GetFreeWayOrder.Info info) throws Exception {

            super.onSuccess(toast, type, info);

            username.setText("用户名 ："+info.name);

            plate.setText(info.plate);

            licensee.setText(info.licensee);

            if(info.state.equals("1")){

                state.setText("已开通");

            }else {

                state.setText("未开通");

            }

            if(info.msg.equals("000")){

                orderList.clear();

                orderList.addAll(info.freewayList);

                for(int i = 0;i<orderList.size();i++){

                    if(!orderList.get(i).meilage.equals("")&&!orderList.get(i).meilage.equals("null")) {

                        meilage += Double.parseDouble(orderList.get(i).meilage);

                    }

                    if(!orderList.get(i).money.equals("")&&!orderList.get(i).money.equals("null")) {

                        price += Double.parseDouble(orderList.get(i).money);

                    }

                    if(!orderList.get(i).end_time.equals("")&&!orderList.get(i).end_time.equals("null")) {

                        time += (Long.parseLong(orderList.get(i).end_time) - Long.parseLong(orderList.get(i).start_time)) / 1000 / 60;

                    }

                }

                if(time/60>0){

                    date = time/60+"小时"+time%60+"分钟";

                }else {

                    date = String.valueOf(time)+"分钟";

                }

                tv_mileage.setText(Tools.F_num(String.valueOf(meilage))+"公里");

                tv_money.setText(Tools.F_num(String.valueOf(price))+"元");

                tv_time.setText(date);


            }else {

                UtilToast.show(context,info.msg);

            }
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_freeway_service);

        initView();

    }

    private void initView() {

        (title_name = (TextView) findViewById(R.id.tv_title_name)).setText("高速服务");

        state = (TextView)findViewById(R.id.tv_freeway_state) ;

        username = (TextView)findViewById(R.id.tv_freeway_username) ;

        plate = (TextView)findViewById(R.id.tv_freeway_plate) ;

        licensee = (TextView)findViewById(R.id.tv_freeway_licensee) ;

        tv_mileage = (TextView)findViewById(R.id.tv_freeway_serrvice_mileage) ;

        tv_time = (TextView)findViewById(R.id.tv_freeway_serrvice_time) ;

        tv_money = (TextView)findViewById(R.id.tv_freeway_serrvice_money) ;

        (check_order = (TextView)findViewById(R.id.tv_freeway_check_order)).setOnClickListener(this);

        getFreeWayOrder.execute(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.tv_freeway_check_order:

                startActivity(new Intent(this,FreeWayOrderInfoActivity.class));

                break;

        }

    }


}
